/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ssp.net;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import ssp.topology.Node;
import ssp.utility.MersenneTwisterRNG;
import ssp.utility.PropertyManager;

/**
 *
 * @author yamazaki
 */
public class NWresourceFactory {
    
    /**
     * ネットワークコンポーネントに資源を割り当てる
     * @param QoSnum QoS指標の数
     * @param QoSrange QoS指標の幅
     */
    public static void allocateNWresource(int QoSnum, int QoSrange) {
        
        double bandwidthUnit = PropertyManager.getValueAsDouble("BandwidthUnit");
        int capacity = PropertyManager.getValueAsInteger("Bandwidth");
    
        for(NWcomponent nwComponent : NWcomponentManager.getINSTANCE().getNWcomponentList()) {
            List<Node> neighbors = new ArrayList<Node>(nwComponent.getNeighborNWcomponents());
            
            for(int i=0 ; i<neighbors.size() ; i++) {
                for(int j=i ; j< neighbors.size() ; j++) {
                    
                    Set<Node> connectedNode = new HashSet<Node>();
                    connectedNode.add(neighbors.get(i));
                    connectedNode.add(neighbors.get(j));
                    
                    List<Integer> QoSindexList = new ArrayList<Integer>(makeQoS(QoSnum, QoSrange));
                    
                    nwComponent.getNWresourceManager().addNWresourceEntry(new NWresourceEntry(connectedNode, bandwidthUnit, makecharge(bandwidthUnit, QoSindexList), QoSindexList, capacity));
                }
            }
        }
    }
    
    /**
     * 指定数のQoS指標を生成
     * @param QoSnum QoSの数
     * @param QoSrange QoSの幅
     * @return 
     */
    private static List<Integer> makeQoS(int QoSnum, int QoSrange) {
        List<Integer> QoSindexList = new ArrayList<Integer>();
        for(int i=0 ; i<QoSnum ; i++) {
            QoSindexList.add(MersenneTwisterRNG.getINSTANCE().nextInt(QoSrange));
        }
        return QoSindexList;
    }
    
    private static double makecharge(double bandwidthUnit, List<Integer> QoSindexList) {
        double charge = bandwidthUnit * MersenneTwisterRNG.getINSTANCE().nextDouble();
        for(int qos : QoSindexList) {
            charge += qos * MersenneTwisterRNG.getINSTANCE().nextDouble();
        }
        return charge;
    }
    
}
